//
//  WebHelper.m
//  Rail
//
//  Created by hychen on 2018/5/24.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "WebHelper.h"

@implementation WebHelper

//return NO不拦截 YES拦截
+ (BOOL)ProcessDeepLinkWithUrl:(NSString *)url vc:(UIViewController *)vc {
    if (![url isKindOfClass:[NSString class]]) {
        return NO;
    }
    
    if ([url containsString:kWebDeepLinkArea]) {
        NSString *areaId = [[self class] getJumpValueWithUrl:url type:@"1"];
        if ([areaId objectIsNotEmpty]) {
            [JumpTool gotoWorkAreaDetailWithAreaId:areaId navTitle:@"工区详情" vc:vc];
        }
        return YES;
    }
    
    if ([url containsString:kWebDeepLinkStation]) {
        NSString *stationId = [[self class] getJumpValueWithUrl:url type:@"2"];
        if ([stationId objectIsNotEmpty]) {
            [JumpTool gotoStationDetailWithStationId:stationId vc:vc];
        }
        return YES;
    }
    
    if ([url containsString:kWebDeepLinkSection]) {
        NSString *sectionId = [[self class] getJumpValueWithUrl:url type:@"3"];
        if ([sectionId objectIsNotEmpty]) {
            [JumpTool gotoSectionDetailWithSectionId:sectionId vc:vc];
        }
        return YES;
    }
    
    if ([url containsString:kWebDeepLinkLine]) {
        NSString *lineId = [[self class] getJumpValueWithUrl:url type:@"4"];
        if ([lineId objectIsNotEmpty]) {
            [JumpTool gotoLineDetailWithLineId:lineId vc:vc];
        }
        return YES;
    }
    
    return NO;
}

// type:1工区 2站点 3标段 4线路
+ (NSString *)getJumpValueWithUrl:(NSString *)url type:(NSString *)type{
    if ([type isEqualToString:@"1"]) {
        //工区
        NSRange range = [url rangeOfString:kWebDeepLinkArea options:NSCaseInsensitiveSearch];
        if (range.location != NSNotFound) {
            return [url substringFromIndex:range.location+range.length];
        }
    }
    else if ([type isEqualToString:@"2"]) {
        //站点
        NSRange range = [url rangeOfString:kWebDeepLinkStation options:NSCaseInsensitiveSearch];
        if (range.location != NSNotFound) {
            return [url substringFromIndex:range.location+range.length];
        }
    }
    else if ([type isEqualToString:@"3"]) {
        //标段
        NSRange range = [url rangeOfString:kWebDeepLinkSection options:NSCaseInsensitiveSearch];
        if (range.location != NSNotFound) {
            return [url substringFromIndex:range.location+range.length];
        }
    }
    else if ([type isEqualToString:@"4"]) {
        //线路
        NSRange range = [url rangeOfString:kWebDeepLinkLine options:NSCaseInsensitiveSearch];
        if (range.location != NSNotFound) {
            return [url substringFromIndex:range.location+range.length];
        }
    }
    return @"";
}
@end
